//
//  JMHelper.m
//  YaoYao
//
//  Created by JM Zhao on 2016/11/21.
//  Copyright © 2016年 JunMingZhaoPra. All rights reserved.
//

#import "JMHelper.h"
#import <AVFoundation/AVFoundation.h>

@implementation JMHelper

+ (UIImage *)getVideoPreViewImage:(NSURL *)videoPath
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoPath options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *img = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return img;
}

// 第几天
+ (NSInteger)dayFromluanch
{
    NSDate *senddate = [NSDate date];
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString *nowTimesp = [NSString stringWithFormat:@"%ld",(long)[senddate timeIntervalSince1970]];
    NSInteger number = [nowTimesp integerValue] - [[user objectForKey:@"timeMark"] integerValue];
    return number / (24*3600)+1;
}

+ (BOOL)isVIP
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    return [user boolForKey:@"superUser"];
}

+ (BOOL)isFree
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSDate *senddate = [NSDate date];
    NSString *nowTimesp = [NSString stringWithFormat:@"%ld",(long)[senddate timeIntervalSince1970]];
    if (([nowTimesp integerValue] - [[user objectForKey:@"timeMark"] integerValue] < 172800) || [self isVIP]){// 172800
        
        return YES;
        
    }else {
        
        if ([self isVIP]) {
            
            return YES;
            
        }else{
            
            return NO;
        }
    }
}

+ (BOOL)touchNumber
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSInteger touchNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"touchNumber"];
    
    if (touchNumber %2 == 0) {
        
        touchNumber ++;
        [user setInteger:touchNumber forKey:@"touchNumber"];
        return YES;
        
    }else{
        
        touchNumber ++;
        [user setInteger:touchNumber forKey:@"touchNumber"];
        return NO;
    }
}

+ (BOOL)isShowPayB
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSInteger touchNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"isShowPayB"];
    
    if (touchNumber %2 == 0) {
        
        touchNumber ++;
        [user setInteger:touchNumber forKey:@"isShowPayB"];
        return YES;
        
    }else{
        
        touchNumber ++;
        [user setInteger:touchNumber forKey:@"isShowPayB"];
        return NO;
    }
}

+ (BOOL)isShowFiveStar
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSInteger touchNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"isShowFiveStar"];
    
    if (touchNumber %5 == 0) {
        
        touchNumber ++;
        [user setInteger:touchNumber forKey:@"isShowFiveStar"];
        return YES;
        
    }else{
        
        touchNumber ++;
        [user setInteger:touchNumber forKey:@"isShowFiveStar"];
        return NO;
    }
}

+ (BOOL)VideoTouchNumber
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSInteger touchNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"VideotouchNumber"];
    
    if (touchNumber %2 == 0) {
        
        touchNumber ++;
        [user setInteger:touchNumber forKey:@"VideotouchNumber"];
        return YES;
        
    }else{
        
        touchNumber ++;
        [user setInteger:touchNumber forKey:@"VideotouchNumber"];
        return NO;
    }
}

+ (NSString *)confromTimesp
{
    NSDate *senddate = [NSDate date];
    return [NSString stringWithFormat:@"%ld",(long)[senddate timeIntervalSince1970]];
}

// 创建文件夹
+ (NSString *)creatFolder:(NSString *)name dir:(NSString *)dir
{
    BOOL isDir = NO;
    NSString *pathDir = [dir stringByAppendingPathComponent:name];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:pathDir isDirectory:&isDir];
    
    if ( !(isDir == YES && existed == YES) )
    {
        BOOL isSuccess = [fileManager createDirectoryAtPath:pathDir withIntermediateDirectories:YES attributes:nil error:nil];
        
        if (isSuccess) {
            
            return pathDir;
        }else{
        
            return nil;
        }
    }else{
    
        return nil;
    }
}

+ (NSMutableArray *)getFileFromDir:(NSString *)dir bySuffix:(NSString *)suffix
{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSError *error;
    NSArray *array = [manger contentsOfDirectoryAtPath:dir error:&error];
    NSMutableArray *pngs = [NSMutableArray array];
    if (!error) {
        
        for (NSString *name in array) {
            
            if ([name hasSuffix:suffix]) {
                
                [pngs addObject:[dir stringByAppendingPathComponent:name]];
            }
        }
    }
    
    return pngs;
}

+ (NSMutableArray *)getFilesFromDir:(NSString *)dir
{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSError *error;
    NSArray *array = [manger contentsOfDirectoryAtPath:dir error:&error];
    NSMutableArray *pngs = [NSMutableArray array];
    if (!error) {
        
        for (NSString *name in array) {
            
            [pngs addObject:[dir stringByAppendingPathComponent:name]];
        }
    }
    
    return pngs;
}

+ (NSMutableArray *)getPhotoPath:(NSString *)photoPath
{
    NSError *error;
    NSArray *dirs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:photoPath error:&error];
    
    NSMutableArray *photos = [NSMutableArray array];
    for (NSString *path in dirs) {
        
        if (![path isEqualToString:@".DS_Store"]) {
            
            [photos addObject:path];
        }
    }
    return photos;
}

@end
